package com.server.push.ws;


import com.server.dao.MobileAppDao;
import com.server.dao.MobileAppTrackDao;
import com.server.pojo.MobileApp;
import com.server.pojo.MobileAppTrack;
import com.server.http.service.MobileAppService;
import com.server.libs.PositionUtil;
import com.server.push.dto.request.RequestMsg;
import com.server.push.ws.base.WsSessions;
import com.server.push.dto.request.data.RequestLocation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.websocket.Session;
import java.util.*;

@Component
@Slf4j
public class WebsocketMsg {

    public static Set<Session> allSessions = new HashSet<>();
    /* 存储mobileApp的Session */
    public static Map<Integer,Session> mobileAppSessions = new HashMap<>();

    @Autowired
    MobileAppDao mobileAppDao;

    @Autowired
    StringRedisTemplate redis;


    @Autowired
    MobileAppService mobileAppService;

    @Autowired
    MobileAppTrackDao mobileAppTrackDao;

    @Transactional
    public void onMessage(RequestMsg requestMsg, Session session) {



        /* 查询连接用户MobileApp */
        String auth = requestMsg.getAuth();
        MobileApp mobileApp = mobileAppService.get(auth);

        /* 记录所有连接 */
        WsSessions.all.add(session);

        if (mobileApp == null) {
            return;
        }

        Integer id = mobileApp.getId();

        if(WsSessions.mobileApps.containsKey(id)){
            if(!WsSessions.mobileApps.get(id).equals(session)){
                log.info("账号：" + mobileApp.getAccount() + " 重新连接 WebSocket");
            }
        }else {
            log.info("账号：" + mobileApp.getAccount() + " 连接 WebSocket");
        }
        WsSessions.mobileApps.put(id, session);
//        InetSocketAddress addr = WsPush.getAddr(session);
//        mobileApp.setIp(addr.getHostName());
//        mobileApp.setPort(addr.getPort());
        mobileApp.setOnlineAt(new Date());


        if (requestMsg.getType().equals("Location")){
            RequestLocation data = requestMsg.getData(RequestLocation.class);
            if (data.getLatitude() != null && data.getLongitude() != null) {
                mobileApp.setLatitude(data.getLatitude());
                mobileApp.setLongitude(data.getLongitude());

                if (mobileApp.getTrack().size() == 0){
                    MobileAppTrack mobileAppTrack = new MobileAppTrack().setMobileApp(mobileApp);
                    mobileAppTrackDao.save(mobileAppTrack);
                }else {
                    MobileAppTrack last = mobileApp.getTrack().get(0);
                    double v = PositionUtil.calculateDistance(last.getLatitude(), last.getLongitude(), mobileApp.getLatitude(), mobileApp.getLongitude());
                    if (v>1) {
                        MobileAppTrack mobileAppTrack = new MobileAppTrack().setMobileApp(mobileApp);
                        mobileAppTrackDao.save(mobileAppTrack);
                    }
                }




            }
        }

        mobileAppDao.save(mobileApp);


    }


    @Transactional
    public void onError(Session session, Throwable error) {
        for (Map.Entry<Integer, Session> entry : WsSessions.mobileApps.entrySet()) {
            Integer id = entry.getKey();
            Session session1 = entry.getValue();
            if (session1.equals(session)) {
                log.error("异常错误: " + error.toString());
            }
        }
    }



}
